package com.weiyuwqj.admin.controller.admin.invest;

import com.weiyuwqj.admin.controller.admin.log.annotation.LogAnnotation;
import com.weiyuwqj.api.service.SysContractsService;
import com.weiyuwqj.api.service.BusInvestRecordService;
import com.weiyuwqj.api.service.BusInvestService;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.SysContracts;
import com.weiyuwqj.api.model.convert.*;
import com.weiyuwqj.api.model.vo.BusItemInvestVo;
import com.weiyuwqj.api.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by yxw on 2015/12/28.
 */
@Controller
@RequestMapping(value = "/admin/invest")
public class BusInvestRecordController {
    @Autowired
    BusInvestRecordService recordService;


    @Autowired
    BusInvestService investService;

    @Autowired
    SysContractsService contractsService;


    /**
     * 显示&查询
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/recordList", method = RequestMethod.POST)
    public Object recordList(Paginator paginator, HttpServletRequest request){
        String userId = request.getParameter("userId");
        String itemname = request.getParameter("itemname");
        String name = request.getParameter("name");
        String mobile = request.getParameter("mobile");
        String beginDateTime = request.getParameter("beginDateTime");
        String endDateTime = request.getParameter("endDateTime");
        String ticketUseStatus = request.getParameter("ticketUseStatus");
        String couponUseStatus = request.getParameter("couponUseStatus");
        String investType = request.getParameter("investType");
        String orderName = request.getParameter("orderName");
        String orderWay = request.getParameter("orderWay");
        String customerType = request.getParameter("customerType");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("itemname", itemname);
        map.put("name", name);
        map.put("mobile", mobile);
        map.put("beginDateTime", beginDateTime);
        map.put("endDateTime", endDateTime);
        map.put("userId",userId);
        map.put("ticketUseStatus", ticketUseStatus);
        map.put("couponUseStatus", couponUseStatus);
        map.put("orderName", orderName);
        map.put("orderWay",orderWay);
        map.put("customerType",customerType);
        map.put("investType",investType);
        PageList l=recordService.getList(paginator, map);
        return l;
    }


    @ResponseBody
    @RequestMapping("/excel/invest")
    @LogAnnotation(module ="投资管理/投资记录",description = "导出投资记录")
    public Object investExcel(HttpServletRequest request,
                              String userId,
                              String itemname,
                              String name,
                              String mobile,
                              String beginDateTime,
                              String endDateTime,
                              String ticketUseStatus,
                              String couponUseStatus,
                              String customerType,
                              String investType){

        String[] cArray={"项目名称","用户名","手机号","真实姓名","用户类型","投资类型","投资状态","流水号","投资金额","成交金额","应收总额","应收本金","应收利息",
                "期限","期限单位","利率","加息利率","红包金额","红包名称"," 加息劵利率","加息券名称","投资时间","满标时间","回款时间","合同地址"};
        String[] eArray={"ItemName","Username","Mobile","Realname","CustomerType","InvestType","InvestStatus","InvestOrder","InvestAmount"
                ,"InvestDealAmount","CollectAmount", "CollectPrincipal","CollectInterest","ItemCycle","ItemCycleUnit","ItemRate"
                ,"ItemAddRate","CouponAmount","CouponName","TicketRate","TicketName","Addtime","FullTime","ReceivableTime","ContractsUrl"};
        ModelConvert[] converts={new ItemNameConvert(),null,null,null,new CustomerTypeConvert(),new InvestTypeConvert(),
                new InvestStatusConvert(),null,null,null,null,null,null,new ItemCycleConvert(),
                new ItemCycleUnitConvert(),null,null,new CouponUseConvert(),new CouponUseConvert(),
                new CouponUseConvert(),new CouponUseConvert(),new InvestAddtimeConvert(),new InvestAddtimeConvert(),new InvestAddtimeConvert(),null};

        List<String> cList= Arrays.asList(cArray);
        List<String> eList=Arrays.asList(eArray);
        List<ModelConvert> convertList=Arrays.asList(converts);
        Map map=new HashMap();
        map.put("itemname",itemname);
        map.put("name",name);
        map.put("mobile",mobile);
        map.put("beginDateTime",beginDateTime);
        map.put("endDateTime",endDateTime);
        map.put("ticketUseStatus", ticketUseStatus);
        map.put("couponUseStatus", couponUseStatus);
        map.put("userId",userId);
        map.put("customerType",customerType);
        map.put("investType",investType);

        List<BusItemInvestVo> contentList=recordService.getList(map);
        String serverDir=request.getServletContext().getRealPath("/")+"admin/export/";
        String excelFilePath=ExcelUtils.export("投资记录",cList,eList,convertList,contentList,serverDir);
        HashMap ret= Message.OK;
        ret.put("file","export/"+excelFilePath);
        return ret;
    }

    @ResponseBody
    @RequestMapping(value="/recordByItemId", method = RequestMethod.POST)
    public Object getInvestByItemId(int itemId,Paginator paginator){
        List list =recordService.getInvestByItemId(itemId,paginator);
        return new PageList(list);
    }

    //根据userId获取投资记录
    @ResponseBody
    @RequestMapping(value="/getListByUserId", method = RequestMethod.POST)
    public Object getListByUserId(Paginator p,Integer userId){
        return recordService.getListByUserId(p,userId);
    }



    @ResponseBody
    @RequestMapping("/statInvest")
    public Object statInvest(){
        Map map=new HashMap();
        map.put("investAccount",investService.sumInvestAccount());
        map.put("investInterest",investService.sumInvestInterest());
        map.put("investPrincipal",investService.sumInvestPrincipal());
        return map;
    }

    @ResponseBody
    @RequestMapping("/getContractByInvestId")
    public Object getContractByInvestId(Integer investId){
        SysContracts contracts=contractsService.getContractsByInvestId(investId);
        return contracts;
    }

}
